<?php

namespace Home\Model;

use Think\Model;

class ShopModel extends Model {

	/**
	 * 保存数据
	 *
	 * @param unknown $data
	 * @param unknown $id
	 * @param string $uid
	 * @return Ambigous <string, boolean, unknown>
	 */
	public function saveData($data, $id, $uid = '') {
		if ($id) {
			// 编辑
			if ($data ['name']) {
				$_data ['name'] = $data ['name'];
			}
			if ($data ['addr']) {
				$_data ['addr'] = $data ['addr'];
			}
			if ($data ['phone']) {
				$_data ['phone'] = $data ['phone'];
			}
			if ($data ['qq']) {
				$_data ['qq'] = $data ['qq'];
			}
			$where = 'id=' . intval ( $id );
			if ($uid) {
				$where .= 'and uid=' . intval ( $uid );
			}
			$rs = $this->where ( $where )->save ( $_data );
			if ($rs) {
				$result ['status'] = 1;
				$result ['msg'] = '编辑成功';
				$result ['id'] = $rs;
			} else {
				$result ['status'] = 0;
				$result ['msg'] = '没有任何操作';
			}
		} else {
			// 新增
			if (! $data ['name']) {
				$result ['status'] = 0;
				$result ['msg'] = '名称不能空';
			} elseif (! $data ['addr']) {
				$result ['status'] = 0;
				$result ['msg'] = '地址不能空';
			} elseif (! $data ['phone']) {
				$result ['status'] = 0;
				$result ['msg'] = '电话不能空';
			} elseif (! $data ['qq']) {
				$result ['status'] = 0;
				$result ['msg'] = 'QQ不能空';
			} else {
				$data ['uid'] = get_cookie ( 'uid' );
				$data ['createtime'] = date ( 'Y-m-d H:i:s' );
				$data ['createip'] = get_user_ip ();
				$rs = $this->add ( $data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['msg'] = $rs;
					$result ['id'] = $rs;
				} else {
					$result ['status'] = 0;
					$result ['msg'] = '添加失败';
				}
			}
		}
		return $result;
	}

	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = 12) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$result ['list'] = $this->field ( 'id' )->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] = $this->getInfo ( $v ['id'] );
		}
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		return $result;
	}

	/**
	 * 获取单条信息
	 *
	 * @param unknown $id
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
	 */
	public function getInfo($id) {
		$where = 'id=' . intval ( $id );
		$result = $this->where ( $where )->find ();
		$result ['url'] = '/?a=blogshow&id=' . $result ['id'];
		$result ['text200'] = cut_str ( strip_tags ( $result ['intro'] ), 200, '' );
		$result ['createdata'] = date ( 'Y-m-d', strtotime ( $result ['createtime'] ) );
		return $result;
	}
	public function getInfoForUid($uid) {
		$where = 'uid=' . intval ( $uid );
		$rs = $this->search ( 'uid=' . intval ( $uid ) );
		$result = $rs ['list'] ['0'];
		return $result;
	}
}
